#!/bin/sh
# $1 has the path to the source package
# $2 has the path to the target folder
# $3 has the path to the target volume
# $USER has the name of the user who launched the installer


if [ -e "/Applications/Utilities/LCC Connection Utility.app" ]; then
	/usr/sbin/chown -R root:admin "/Applications/Utilities/LCC Connection Utility.app"
	/bin/chmod -R ug+rw,o-w "/Applications/Utilities/LCC Connection Utility.app"
fi

if [ -e "/Applications/Utilities/LCC Uninstaller.app" ]; then
	/usr/sbin/chown -R root:admin "/Applications/Utilities/LCC Uninstaller.app"
	/bin/chmod -R ug+rw,o-w "/Applications/Utilities/LCC Uninstaller.app"
fi

if [ -e "/Library/Application Support/Logitech.localized" ]; then
	/usr/sbin/chown -R root:admin "/Library/Application Support/Logitech.localized"
	/bin/chmod -R ug+rw,o-w "/Library/Application Support/Logitech.localized"
fi

if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app" ]; then
	/usr/sbin/chown root:admin "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app/Contents/Resources/SetPriority"
	/bin/chmod u+s "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app/Contents/Resources/SetPriority"
fi

if [ -e "/Library/PreferencePanes/Logitech Control Center.prefPane" ]; then
	/usr/sbin/chown -R root:admin "/Library/PreferencePanes/Logitech Control Center.prefPane"
	/bin/chmod -R ug+rw,o-w "/Library/PreferencePanes/Logitech Control Center.prefPane"
	/bin/chmod ugo+w "/Library/PreferencePanes/Logitech Control Center.prefPane"/Contents/Resources/*.lproj/html/
	/bin/chmod ugo+w "/Library/PreferencePanes/Logitech Control Center.prefPane"/Contents/Resources/*.lproj/html/warranty_keyboards.htm
	/bin/chmod ugo+w "/Library/PreferencePanes/Logitech Control Center.prefPane"/Contents/Resources/*.lproj/html/warranty_mice.htm
elif [ -e "/Library/PreferencePanes/Logicool Control Center.prefPane" ]; then
	/usr/sbin/chown -R root:admin "/Library/PreferencePanes/Logicool Control Center.prefPane"
	/bin/chmod -R ug+rw,o-w "/Library/PreferencePanes/Logicool Control Center.prefPane"
	/bin/chmod ugo+w "/Library/PreferencePanes/Logicool Control Center.prefPane"/Contents/Resources/*.lproj/html/
	/bin/chmod ugo+w "/Library/PreferencePanes/Logicool Control Center.prefPane"/Contents/Resources/*.lproj/html/warranty_keyboards.htm
	/bin/chmod ugo+w "/Library/PreferencePanes/Logicool Control Center.prefPane"/Contents/Resources/*.lproj/html/warranty_mice.htm
fi

if [ -e "/Library/InputManagers/LCC Scroll Enhancer Loader" ]; then
	/usr/sbin/chown root:wheel "/Library/InputManagers"
	/usr/sbin/chown -R root:wheel "/Library/InputManagers/LCC Scroll Enhancer Loader"
	/bin/chmod -R ugo+r,go-w "/Library/InputManagers/LCC Scroll Enhancer Loader"
fi

if [ -e "/Library/ScriptingAdditions/LCC Scroll Enhancer Loader.osax" ]; then
	/usr/sbin/chown root:admin "/Library/ScriptingAdditions"
	/usr/sbin/chown -R root:admin "/Library/ScriptingAdditions/LCC Scroll Enhancer Loader.osax"
	/bin/chmod -R ug+rw,o-w "/Library/ScriptingAdditions/LCC Scroll Enhancer Loader.osax"
fi

# be sure that the system realizes that something changed, and rebuild extension cache

if [ -f "/Users/$USER/Library/Caches/.ApplePrefPanesCache" ]; then
	/bin/rm -f "/Users/$USER/Library/Caches/.ApplePrefPanesCache"
fi
if [ -f "/Users/$USER/Library/Caches/com.apple.preferencepanes.cache" ]; then
	/bin/rm -f "/Users/$USER/Library/Caches/com.apple.preferencepanes.cache"
fi
/usr/bin/touch "/Library/PreferencePanes"
/usr/bin/touch "/System/Library/PreferencePanes"
/usr/bin/touch "/Library/Keyboard Layouts"

if [ -e "$1/Contents/Resources/LCC Uninstaller Tool" ] ; then
	"$1/Contents/Resources/LCC Uninstaller Tool" --postflight
fi

"$1/Contents/Resources/RMover" "$1"

# now the extension is placed at the correct place. be sure it has the correct owner

if [ -e "/System/Library/Extensions/LogitechHIDDevices.kext" ] ; then
	/usr/sbin/chown -R root:wheel "/System/Library/Extensions/LogitechHIDDevices.kext"
	/bin/chmod -R ug+r,go-w "/System/Library/Extensions/LogitechHIDDevices.kext"
elif [ -e "/System/Library/Extensions/LogicoolHIDDevices.kext" ] ; then
	/usr/sbin/chown -R root:wheel "/System/Library/Extensions/LogicoolHIDDevices.kext"
	/bin/chmod -R ug+r,go-w "/System/Library/Extensions/LogicoolHIDDevices.kext"
fi

if [ -e "/System/Library/Extensions/LogitechUnifying.kext" ] ; then
	/usr/sbin/chown -R root:wheel "/System/Library/Extensions/LogitechUnifying.kext"
	/bin/chmod -R ug+r,go-w "/System/Library/Extensions/LogitechUnifying.kext"
elif [ -e "/System/Library/Extensions/LogicoolUnifying.kext" ] ; then
	/usr/sbin/chown -R root:wheel "/System/Library/Extensions/LogicoolUnifying.kext"
	/bin/chmod -R ug+r,go-w "/System/Library/Extensions/LogicoolUnifying.kext"
fi

if [ -f "/System/Library/Extensions.mkext" ]; then
	/bin/rm -f "/System/Library/Extensions.mkext"
fi
/usr/bin/touch "/System/Library/Extensions"

# /Applications/Utilities/Logitech Updater.app
if [ -e "/Applications/Utilities/Logitech Updater.app" ]; then
	/usr/sbin/chown root:wheel "/Applications/Utilities/Logitech Updater.app/Contents/Resources/Logitech Installer Tool"
	/bin/chmod ug=rwxs,o=rxs "/Applications/Utilities/Logitech Updater.app/Contents/Resources/Logitech Installer Tool"
fi

# Add eReg to login items

if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logitech Registration.app" ] ; then
	sudo -u $USER /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logitech Registration.app\", hidden: false } at end"
elif [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logicool Registration.app" ] ; then
	sudo -u $USER /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logicool Registration.app\", hidden: false } at end"
fi

# Launch eReg

#if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logitech Registration.app" ] ; then
#	sudo -u $USER -b /Library/Application\ Support/Logitech.localized/Logitech\ Control\ Center.localized/Logitech\ Registration.app/Contents/MacOS/Logitech /regonly /regdelay=48 /surveydelay=360
#	sudo -u $USER -b /Library/Application\ Support/Logitech.localized/Logitech\ Control\ Center.localized/Logitech\ Registration.app/Contents/MacOS/Logitech
#elif [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logicool Registration.app" ] ; then
#	sudo -u $USER -b /Library/Application\ Support/Logitech.localized/Logitech\ Control\ Center.localized/Logicool\ Registration.app/Contents/MacOS/Logicool /regonly /regdelay=48 /surveydelay=360
#	sudo -u $USER -b /Library/Application\ Support/Logitech.localized/Logitech\ Control\ Center.localized/Logicool\ Registration.app/Contents/MacOS/Logicool
#fi

# Add the daemon to the login items

if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app" ] ; then
	sudo -u $USER /usr/bin/osascript -e "tell application \"System Events\" to delete (every login item whose path ends with \"LCCDaemon.app\")"
	sudo -u $USER /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app\", hidden: true } at end"
fi

# Launch the daemon

#if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app" ] ; then
#	sudo -u $USER -b /Library/Application\ Support/Logitech.localized/Logitech\ Control\ Center.localized/LCCDaemon.app/Contents/MacOS/LCCDaemon
#fi

exit 0
